﻿using AutoMapper;
using Domain.Factory_Modeling;
using Infratructure.Interface;
using MediatR;
using ReturnValue;

namespace MES.Server.API.Application.Command.Factory_Modeling
{
    public class DelProductionLineHander : IRequestHandler<DelProductionLineCommand, ApiResult<int>>
    {
        private readonly IRepository<ProductionLineModel> productionLineRepo;
        private readonly IMapper mapper;
        public DelProductionLineHander(IRepository<ProductionLineModel> productionLineRepo, IMapper mapper)
        {
            this.productionLineRepo = productionLineRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 产线删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(DelProductionLineCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            // 批删逻辑
            if (request.Ids != null && request.Ids.Any())
            {
                var productionLines = new List<ProductionLineModel>();
                var notFoundIds = new List<long>();

                foreach (var id in request.Ids)
                {
                    var productionLine = await productionLineRepo.GetValue(id);
                    if (productionLine == null)
                    {
                        notFoundIds.Add(id);
                    }
                    else
                    {
                        productionLine.IsDel = true;
                        productionLines.Add(productionLine);
                    }
                }

                if (productionLines.Any())
                {
                    await productionLineRepo.UpdateRangeAsync(productionLines);
                    result.Code = StatusCode.Success;
                    result.Message = $"成功删除{productionLines.Count}个产线信息";
                    result.Data = productionLines.Count;

                    if (notFoundIds.Any())
                    {
                        result.Message += $",以下产线不存在: {string.Join(",", notFoundIds)}";
                    }
                }
                else
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "没有找到可删除的产线信息";
                }
            }
            else
            {
                var productionLine = await productionLineRepo.GetValue(request.Id);
                if (productionLine == null)
                {
                    result.Code = StatusCode.Fail;
                    result.Message = "产线不存在,无法删除";
                    return await Task.FromResult(result);
                }
                else
                {
                    productionLine.IsDel = true;
                    await productionLineRepo.UpdateAsync(productionLine);
                    result.Code = StatusCode.Success;
                    result.Message = "删除产线信息成功";
                    return await Task.FromResult(result);
                }
               
            }
            return await Task.FromResult(result);
        }
    }
}
